package com.mbt.remote.server;

import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;

import com.mbt.remote.server.event.MobileEventsServer;
import com.mbt.remote.server.mobile.MobileImageServer;

/**
 * The Class RemoteServer.
 */
public class RemoteServer
{

	/** The running. */
	private static boolean running = true;
	
	/** The im port. */
	private int imPort;
	
	/** The ev port. */
	private int evPort;
	
	
	/** The start. */
	private MenuItem start = null;
	
	/** The stop. */
	private MenuItem stop = null;
	
	/** The exit. */
	private MenuItem exit = null;
	
	private static boolean imageSentLock = false;
	private static boolean reloadImg = true;
	
	public static boolean isReloadImg() {
		return reloadImg;
	}

	public static void setReloadImg(boolean reloadImg) {
		RemoteServer.reloadImg = reloadImg;
	}

	public static boolean isImageSentLock() {
		return imageSentLock;
	}

	public static void setImageSentLock(boolean imageSentLock) {
		RemoteServer.imageSentLock = imageSentLock;
	}

	/**
	 * Instantiates a new remote server.
	 *
	 * @param imPort the im port
	 * @param evPort the ev port
	 * @throws AWTException the aWT exception
	 */
	public RemoteServer(int imPort, int evPort) throws AWTException
	{
		this.imPort = imPort;
		this.evPort = evPort;
		createTray();
	}
	
	/**
	 * Creates the tray.
	 *
	 * @throws AWTException the aWT exception
	 */
	private void createTray() throws AWTException
	{
		ImageIcon image = new ImageIcon(getClass().getClassLoader().getResource("com/mbt/remote/server/icon.jpg"));
		PopupMenu menu = new PopupMenu();
		
		start = new MenuItem("Start");
		start.addActionListener(new ServerAction(true, false));

		stop = new MenuItem("Stop");
		stop.addActionListener(new ServerAction(false, false));
		
		exit = new MenuItem("Exit");
		exit.addActionListener(new ServerAction(true, false));
		
		menu.add(start);
		menu.add(stop);
		menu.add(exit);
		
		start();
		TrayIcon icon = new TrayIcon(image.getImage(),"Remote Desktop Server",menu);
		SystemTray tray = SystemTray.getSystemTray();
		tray.add(icon);
	}
	
	/**
	 * Stop.
	 */
	public void stop()
	{
		running = false;
		start.setEnabled(true);
		stop.setEnabled(false);
	}
	
	/**
	 * Start.
	 *
	 * @throws AWTException the aWT exception
	 */
	public void start() throws AWTException
	{
		running = true;
		start.setEnabled(false);
		stop.setEnabled(true);
		
		Thread imageServerMobileThread = new Thread(new MobileImageServer());
		imageServerMobileThread.start();
		
		Thread eventMobileServerThread = new Thread(new MobileEventsServer());
		eventMobileServerThread.start();
		
	}
	
	/**
	 * Checks if is running.
	 *
	 * @return true, if is running
	 */
	public static boolean isRunning()
	{
		return running;
	}
	
	/**
	 * The main method.
	 *
	 * @param args the arguments
	 * @throws AWTException the aWT exception
	 */
	public static void main(String[] args) throws AWTException
	{
		if(args.length!=2)
		{
			int imPort = 3000;//Integer.parseInt(args[0]);
			int evPort = 3002;//Integer.parseInt(args[1]);
			RemoteServer rs = new RemoteServer(imPort,evPort);
		}
		else
		{
			System.out.println("InCorrect Arguments");
		}
	}
	
	/**
	 * The Class ServerAction.
	 */
	private class ServerAction extends AbstractAction
	{

		/** The start. */
		private boolean start;
		
		/** The exit. */
		private boolean exit = false;
		
		/**
		 * Instantiates a new server action.
		 *
		 * @param action the action
		 * @param exit the exit
		 */
		public ServerAction(boolean action, boolean exit)
		{
			this.start = action;
			this.exit = exit;
		}
		
		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		public void actionPerformed(ActionEvent arg0)
		{
			if(exit)
			{
				System.exit(0);
			}
			if(start)
			{
				try
				{
					start();
				} catch (AWTException e)
				{
					e.printStackTrace();
				}
			}
			else
			{
				stop();
			}
		}
		
		
	}
	
}
